#!/usr/bin/python
#-*-coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [u'match', u'gco_cmake_var_def']

def match (regexpr, string):
  """
  Return True if string match exactly regexpr else False
  >>> import re
  >>> regexpr = re.compile('[0-9].[0-9]')
  >>> match(regexpr, '1.1')
  True
  >>> match(regexpr, '1.10')
  False
  """
  matchobj = regexpr.match(string)
  if matchobj is None :
    return False
  else :
    return matchobj.group() == string

import re

def gco_cmake_var_def (cmake_varname):
  return re.compile(r"(SET|set)[\s]*\(%s" % cmake_varname)

script_pylot_header = "#pylot ShellManager script version 1.0"

re_float = r"(\-|\+)?[0-9]*\.?[0-9]+([eE](\-|\+)?[0-9]+)?"
re_bool = r"yes|on|no|off|True|False|YES|ON|NO|OFF|TRUE|FALSE|true|false"
re_vector = r"""Vector[\s]*\([\s]*%s[\s]*,?[\s]*[\s]*\)""" % re_float
re_list = []

# num
re_num = '((\+|\-)?[0-9]*(\.?[0-9]*)?)'
# num exp notation
re_enum = '((\+|\-)?[0-9]*(\.[0-9]*e[0-9\-\+]*)?)'
re_anum = '(%s|%s)' % (re_num, re_enum)

co_varname = re.compile(r"[_\-a-zA-Z][_\-a-zA-Z0-9]*")
co_pyvarname = re.compile(r"[_a-zA-Z][_a-zA-Z0-9]*")

co_cmake_varname = re.compile(r"\$\{[a-zA-Z-]*\}")
co_vector = re.compile(re_vector)
co_bool = re.compile(re_bool)
co_int = re.compile(r"[0-9]+")
co_float = re.compile(re_float)

co_cmake_option = re.compile(r".+:(BOOL|STRING|PATH|FILEPATH)=.*")
co_cmake_doc_header = re.compile(r"[_a-zA-Z][_a-zA-Z0-9]*: .*")

co_include_pylot_discr = re.compile(r"#include[\s]+.*pyLotDiscretization.*\.h")
co_include_pylot_discr2 = re.compile(r'"pyLotDiscretization/.*/')

co_pylot_file_block = re.compile(r'//Block.*')
co_file_collection_book = re.compile('\@Book.*')
co_file_collection_groupbook = re.compile('\@GroupBook.*')
co_file_collection_name = re.compile('\@\@Collection.*')
co_comment = re.compile(r"^#.*")

co_i18n_locale = re.compile('[a-z]{2,2}(_[A-Z]{2,3})?')

# Scripts shell
co_script_shell_name = re.compile(".+_[0-9]+\.sh")
co_script_shell_content_l1 = re.compile("^(#!(/usr)?/bin/(sh|bash))")
co_script_shell_content_l2 = re.compile(script_pylot_header)
